import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Buffer} from "buffer";
import {PIID} from "./PIID";
import {AxdrBoolean} from "../asn1.axdr/AxdrBoolean";
import {Unsigned16} from "./Unsigned16";
import {AxdrSequenceOf} from "../asn1.axdr/AxdrSequenceOf";
import {A_ResultNormal} from "./A_ResultNormal";
import {A_ResultRecord} from "./A_ResultRecord";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";


enum Choice {
    _ERR_NONE_SELECTED = -1,
    DAR = 0,
    ATTR = 1,
    RECORD_ATTR = 2,
}

export class SubSeqOfAttr extends AxdrSequenceOf<A_ResultNormal>{


    createListElement(): A_ResultNormal {
        return new A_ResultNormal();
    }

    constructor() {
        super();
    }

    set_length(length: number) {
        super.set_length(length);
    }

}

export class SubSeqOfRecordAttr extends AxdrSequenceOf<A_ResultRecord>{

    createListElement(): A_ResultRecord {
        return new A_ResultRecord();
    }

    constructor() {
        super();
    }

    set_length(length: number) {
        super.set_length(length);
    }

}


export class SubChoiceFrameResp implements AxdrType{

    dataCode : Buffer | null = null;
    dar : AxdrEnum | null = null;
    attr : SubSeqOfAttr | null = null;
    recordAttr : SubSeqOfRecordAttr | null = null;
    choice: Choice = Choice._ERR_NONE_SELECTED;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    setDar(dar : AxdrEnum){
        this.resetChoices();
        this.choice = Choice.DAR;
        this.dar = dar;
    }

    setAttr(attr : SubSeqOfAttr){
        this.resetChoices();
        this.choice = Choice.ATTR
        this.attr = attr;
    }

    setRecordAttr(recordAttr : SubSeqOfRecordAttr){
        this.resetChoices();
        this.choice = Choice.RECORD_ATTR;
        this.recordAttr = recordAttr;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;
        let choosen = new AxdrEnum();
        choosen.set_const();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = choosen.getValue();

        if (this.choice == Choice.DAR) {
            this.dar = new AxdrEnum();
            this.dar.set_const();
            codeLength += this.dar.decode(input);
            return codeLength;
        }else if (this.choice == Choice.ATTR) {
            this.attr = new SubSeqOfAttr();
            codeLength += this.attr.decode(input);
            return codeLength;
        }else if (this.choice == Choice.RECORD_ATTR) {
            this.recordAttr = new SubSeqOfRecordAttr();
            codeLength += this.recordAttr.decode(input);
            return codeLength;
        }
        throw new Error("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    encode(output: ReverseByteArrayOutputStream): number {

        if (this.dataCode != null) {
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;

        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Error("Error encoding AxdrChoice: No item in choice was selected.");
        }

        let codeLength = 0;

        if (this.choice == Choice.RECORD_ATTR) {
            codeLength += this.recordAttr.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(2);
            codeLength += c.encode(output);
            return codeLength;
        }else if (this.choice == Choice.ATTR) {
            codeLength += this.attr.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(1);
            codeLength += c.encode(output);
            return codeLength;
        }else if (this.choice == Choice.DAR) {
            codeLength += this.dar.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(0);
            codeLength += c.encode(output);
            return codeLength;
        }
        throw new Error("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    getChoiceIndex(): Choice {
        return this.choice;
    }

    resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.dar = null;
        this.attr = null;
        this.recordAttr = null;
    }

    toString(): string {
        if (this.choice == Choice.DAR) {
            if (this.dar != null) {
                return "choice: {dar: " + this.dar + "}";
            } else {
                return "choice is dar but dar is null";
            }
        }else if(this.choice == Choice.ATTR){
            if(this.attr != null){
                return "choice: {attr: " + this.attr + "}";
            }else{
                return "choice is attr but attr is null";
            }
        }else if(this.choice == Choice.RECORD_ATTR){
            if(this.recordAttr != null){
                return "choice: {recordAttr: " + this.recordAttr + "}";
            }else{
                return "choice is recordAttr but recordAttr is null";
            }
        }else {
            return "unknown";
        }
    }

}

export class GetResponseNext implements AxdrType{

    dataCode : Buffer | null = null;
    piid_acd : PIID | null = null;
    lastFrameFlag : AxdrBoolean | null = null;
    frameNo : Unsigned16 | null = null;
    frameResp : SubChoiceFrameResp | null = null;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    set_all(piid_acd : PIID, lastFrameFlag : AxdrBoolean, frameNo : Unsigned16, frameResp : SubChoiceFrameResp){
        this.piid_acd = piid_acd;
        this.lastFrameFlag = lastFrameFlag;
        this.frameNo = frameNo;
        this.frameResp = frameResp;
    }

    decode(input: ReverseByteArrayInputStream): number {

        let codeLength = 0;

        this.piid_acd = new PIID();
        this.piid_acd.setNumBit(8);
        codeLength += this.piid_acd.decode(input);

        this.lastFrameFlag = new AxdrBoolean();
        codeLength += this.lastFrameFlag.decode(input);

        this.frameNo = new Unsigned16();
        this.frameNo.set_const();
        codeLength += this.frameNo.decode(input);

        this.frameResp = new SubChoiceFrameResp();
        codeLength += this.frameResp.decode(input);

        return codeLength;
    }

    encode(output: ReverseByteArrayOutputStream): number {
        let codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if(this.frameResp != null && this.frameNo != null && this.lastFrameFlag != null && this.piid_acd != null){
                codeLength = 0;
                codeLength += this.frameResp.encode(output);
                codeLength += this.frameNo.encode(output);
                codeLength += this.lastFrameFlag.encode(output);
                codeLength += this.piid_acd.encode(output);
            }else{
                throw new Error("piid_acd || frameResp || lastFrameFlag || frameNo is null");
            }
        }
        return codeLength;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.piid_acd != null && this.lastFrameFlag != null && this.frameNo != null && this.frameResp != null) {
            return "sequence: {"+ "piid_acd: " + this.piid_acd + ", lastframeflag: " + this.lastFrameFlag + ", frameno: " + this.frameNo + ", frameresp: " + this.frameResp  + "}";
        } else {
            return "piid_acd || frameResp || lastFrameFlag || frameNo is null";
        }
    }

}
